package cn.yuanqiao.manage.service.impl;


import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.bean.BeanValidators;
import cn.yuanqiao.manage.domain.ManageFonds;
import cn.yuanqiao.manage.mapper.ManageFondsMapper;
import cn.yuanqiao.manage.service.IManageFondsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 租户全宗管理Service业务层处理
 * 
 * @author yuanqiao
 * @date 2023-06-11
 */
@Service
public class ManageFondsServiceImpl implements IManageFondsService
{
    private static final Logger log = LoggerFactory.getLogger(ManageFondsServiceImpl.class);

    @Autowired
    private ManageFondsMapper manageFondsMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询租户全宗管理
     * 
     * @param ID 租户全宗管理主键
     * @return 租户全宗管理
     */
    @Override
    public ManageFonds selectManageFondsByID(String ID)
    {
        return manageFondsMapper.selectManageFondsByID(ID);
    }

    /**
     * 查询租户全宗管理列表
     * 
     * @param manageFonds 租户全宗管理
     * @return 租户全宗管理
     */
    @Override
    public List<ManageFonds> selectManageFondsList(ManageFonds manageFonds)
    {
        return manageFondsMapper.selectManageFondsList(manageFonds);
    }

    /**
     * 新增租户全宗管理
     * 
     * @param manageFonds 租户全宗管理
     * @return 结果
     */
    @Override
    public int insertManageFonds(ManageFonds manageFonds)
    {
        manageFonds.setCreateTime(DateUtils.getNowDate());
        ManageFonds manageFonds1 = new ManageFonds();
        manageFonds1.setFondsCode(manageFonds.getFondsCode());
        manageFonds1.setTenantCode(manageFonds.getTenantCode());
        List<ManageFonds> manageFonds2 = manageFondsMapper.selectManageFondsList(manageFonds1);
        if(manageFonds2.size()!=0){
            throw new ServiceException("全宗号已存在");
        }
        return manageFondsMapper.insertManageFonds(manageFonds);
    }

    /**
     * 修改租户全宗管理
     * 
     * @param manageFonds 租户全宗管理
     * @return 结果
     */
    @Override
    public int updateManageFonds(ManageFonds manageFonds)
    {
        Long id = manageFonds.getId();
        ManageFonds manageFonds1 = manageFondsMapper.selectManageFondsByID(String.valueOf(id));
        String fondsCode = manageFonds1.getFondsCode();
        if(!fondsCode.equals(manageFonds.getFondsCode())){
            ManageFonds manageFonds2 = new ManageFonds();
            manageFonds2.setFondsCode(manageFonds.getFondsCode());
            manageFonds2.setTenantCode(manageFonds.getTenantCode());
            List<ManageFonds> manageFonds3 = manageFondsMapper.selectManageFondsList(manageFonds2);
            if(manageFonds3.size()!=0){
                throw new ServiceException("全宗号已存在");
            }
        }
        manageFonds.setUpdateTime(DateUtils.getNowDate());
        return manageFondsMapper.updateManageFonds(manageFonds);
    }

    /**
     * 批量删除租户全宗管理
     * 
     * @param IDs 需要删除的租户全宗管理主键
     * @return 结果
     */
    @Override
    public int deleteManageFondsByIDs(String[] IDs)
    {
        return manageFondsMapper.deleteManageFondsByIDs(IDs);
    }

    /**
     * 删除租户全宗管理信息
     * 
     * @param ID 租户全宗管理主键
     * @return 结果
     */
    @Override
    public int deleteManageFondsByID(String ID)
    {
        return manageFondsMapper.deleteManageFondsByID(ID);
    }

    @Override
    public String importFonds(List<ManageFonds> manageList, Boolean isUpdateSupport, String operName, String tenantId) {

        if (StringUtils.isNull(manageList) || manageList.size() == 0)
        {
            throw new ServiceException("导入全宗数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (ManageFonds fond : manageList)
        {
            try
            {
                fond.setCreateBy(operName);
                fond.setTenantId(tenantId);
                BeanValidators.validateWithException(validator, fond);
                manageFondsMapper.insertManageFonds(fond);
                successNum++;
                successMsg.append("<br/>" + successNum + "、全宗号 " + fond.getFondsCode() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、全宗号 " + fond.getFondsCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }
}
